package com.nwpu.server.admin.controller;

import com.nwpu.server.admin.entity.EmbeddingmethodTitleEntity;
import com.nwpu.server.admin.entity.PipelineTitleEntity;
import com.nwpu.server.admin.service.EmbeddingmethodTitleService;
import com.nwpu.server.admin.service.PipelineTitleService;
import com.nwpu.server.admin.service.impl.PipelineServiceImpl;
import com.nwpu.server.common.response.PageUtils;
import com.nwpu.server.common.response.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @Author: YuXingZh
 * @Date: Created in 20:41 2022-04-23
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("admin/index")
public class IndexController {
    @Autowired
    private EmbeddingmethodTitleService embeddingmethodTitleService;

    @Autowired
    private PipelineTitleService pipelineTitleService;

    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) throws ExecutionException, InterruptedException {
        R ok = R.ok();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        String start = LocalDateTime.now().format(formatter);
        log.info(String.format("start task [%s]", start));
        List<EmbeddingmethodTitleEntity> methods = new ArrayList<>();
        List<PipelineTitleEntity> pipelines = new ArrayList<>();
        CompletableFuture[] futures = new CompletableFuture[2];
        futures[0] = CompletableFuture.runAsync(() -> {
            ok.put("methods", embeddingmethodTitleService.getAll());
        });
        futures[1] = CompletableFuture.runAsync(() -> {
            ok.put("pipelines", pipelineTitleService.getAll());
        });
        CompletableFuture.allOf(futures).get();
        String end = LocalDateTime.now().format(formatter);
        log.info(String.format("end task [%s]", end));

        return ok;
    }
}
